home *** CD-ROM | disk | FTP | other *** search
- package com.sun.java.swing.text;
-
- import com.sun.java.swing.event.DocumentEvent;
- import java.awt.Color;
- import java.awt.Component;
- import java.awt.Font;
- import java.awt.FontMetrics;
- import java.awt.Graphics;
- import java.awt.Rectangle;
- import java.awt.Shape;
-
- public class WrappedPlainView extends BoxView implements TabExpander {
- FontMetrics metrics;
- Segment lineBuffer;
- boolean widthChanging;
- int tabBase;
- int tabSize;
- boolean wordWrap;
- int sel0;
- int sel1;
- Color unselected;
- Color selected;
-
- public WrappedPlainView(Element elem) {
- this(elem, false);
- }
-
- public WrappedPlainView(Element elem, boolean wordWrap) {
- super(elem, 1);
- this.lineBuffer = new Segment();
- this.wordWrap = wordWrap;
- }
-
- protected int calculateBreakPosition(int p0, int p1) {
- this.loadText(p0, p1);
- int p;
- if (this.wordWrap) {
- p = p0 + Utilities.getBreakLocation(this.lineBuffer, this.metrics, this.tabBase, this.tabBase + ((BoxView)this).getWidth(), this, p0);
- } else {
- p = p0 + Utilities.getTabbedTextOffset(this.lineBuffer, this.metrics, this.tabBase, this.tabBase + ((BoxView)this).getWidth(), this, p0);
- }
-
- return p;
- }
-
- public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
- this.updateChildren(e, a);
- }
-
- protected void drawLine(int p0, int p1, Graphics g, int x, int y) {
- try {
- p1 = Math.min(((View)this).getDocument().getLength(), p1);
- if (this.sel0 == this.sel1) {
- this.drawUnselectedText(g, x, y, p0, p1);
- } else if (p0 >= this.sel0 && p0 <= this.sel1 && p1 >= this.sel0 && p1 <= this.sel1) {
- this.drawSelectedText(g, x, y, p0, p1);
- } else if (this.sel0 >= p0 && this.sel0 <= p1) {
- if (this.sel1 >= p0 && this.sel1 <= p1) {
- x = this.drawUnselectedText(g, x, y, p0, this.sel0);
- x = this.drawSelectedText(g, x, y, this.sel0, this.sel1);
- this.drawUnselectedText(g, x, y, this.sel1, p1);
- } else {
- x = this.drawUnselectedText(g, x, y, p0, this.sel0);
- this.drawSelectedText(g, x, y, this.sel0, p1);
- }
- } else if (this.sel1 >= p0 && this.sel1 <= p1) {
- x = this.drawSelectedText(g, x, y, p0, this.sel1);
- this.drawUnselectedText(g, x, y, this.sel1, p1);
- } else {
- this.drawUnselectedText(g, x, y, p0, p1);
- }
-
- } catch (BadLocationException var6) {
- throw new StateInvariantError("Can't render: " + p0 + "," + p1);
- }
- }
-
- protected int drawSelectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
- g.setColor(this.selected);
- Document doc = ((View)this).getDocument();
- doc.getText(p0, p1 - p0, this.lineBuffer);
- return Utilities.drawTabbedText(this.lineBuffer, x, y, g, this, p0);
- }
-
- protected int drawUnselectedText(Graphics g, int x, int y, int p0, int p1) throws BadLocationException {
- g.setColor(this.unselected);
- Document doc = ((View)this).getDocument();
- doc.getText(p0, p1 - p0, this.lineBuffer);
- return Utilities.drawTabbedText(this.lineBuffer, x, y, g, this, p0);
- }
-
- protected final Segment getLineBuffer() {
- return this.lineBuffer;
- }
-
- public float getPreferredSpan(int axis) {
- this.updateMetrics();
- return super.getPreferredSpan(axis);
- }
-
- protected int getTabSize() {
- Integer i = (Integer)((View)this).getDocument().getProperty("tabSize");
- int size = i != null ? i : 8;
- return size;
- }
-
- public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) {
- this.updateChildren(e, a);
- Rectangle alloc = a != null && ((BoxView)this).isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
- int pos = e.getOffset();
- View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
- if (v != null) {
- v.insertUpdate(e, alloc, f);
- }
-
- }
-
- protected void loadChildren(ViewFactory f) {
- Element e = ((View)this).getElement();
- int n = e.getElementCount();
- if (n > 0) {
- View[] added = new View[n];
-
- for(int i = 0; i < n; ++i) {
- added[i] = new WrappedLine(this, e.getElement(i));
- }
-
- ((BoxView)this).replace(0, 0, added);
- }
-
- }
-
- final void loadText(int p0, int p1) {
- try {
- Document doc = ((View)this).getDocument();
- doc.getText(p0, p1 - p0, this.lineBuffer);
- } catch (BadLocationException var4) {
- throw new StateInvariantError("Can't get line text");
- }
- }
-
- public float nextTabStop(float x, int tabOffset) {
- int ntabs = ((int)x - this.tabBase) / this.tabSize;
- return (float)(this.tabBase + (ntabs + 1) * this.tabSize);
- }
-
- public void paint(Graphics g, Shape a) {
- Rectangle alloc = (Rectangle)a;
- this.tabBase = alloc.x;
- JTextComponent host = (JTextComponent)((View)this).getContainer();
- this.sel0 = host.getSelectionStart();
- this.sel1 = host.getSelectionEnd();
- this.unselected = ((Component)host).isEnabled() ? ((Component)host).getForeground() : host.getDisabledTextColor();
- Caret c = host.getCaret();
- this.selected = c.isSelectionVisible() ? host.getSelectedTextColor() : this.unselected;
- g.setFont(((Component)host).getFont());
- super.paint(g, a);
- }
-
- public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) {
- this.updateChildren(e, a);
- Rectangle alloc = a != null && ((BoxView)this).isAllocationValid() ? ((CompositeView)this).getInsideAllocation(a) : null;
- int pos = e.getOffset();
- View v = ((CompositeView)this).getViewAtPosition(pos, alloc);
- if (v != null) {
- v.removeUpdate(e, alloc, f);
- }
-
- }
-
- public void setSize(float width, float height) {
- this.updateMetrics();
- if ((int)width != ((BoxView)this).getWidth()) {
- ((BoxView)this).preferenceChanged((View)null, true, true);
- this.widthChanging = true;
- }
-
- super.setSize(width, height);
- this.widthChanging = false;
- }
-
- void updateChildren(DocumentEvent e, Shape a) {
- Element elem = ((View)this).getElement();
- DocumentEvent.ElementChange ec = e.getChange(elem);
- if (ec != null) {
- Element[] removedElems = ec.getChildrenRemoved();
- Element[] addedElems = ec.getChildrenAdded();
- View[] added = new View[addedElems.length];
-
- for(int i = 0; i < addedElems.length; ++i) {
- added[i] = new WrappedLine(this, addedElems[i]);
- }
-
- ((BoxView)this).replace(ec.getIndex(), removedElems.length, added);
- if (a != null) {
- ((BoxView)this).preferenceChanged((View)null, true, true);
- ((View)this).getContainer().repaint();
- }
- }
-
- this.updateMetrics();
- }
-
- final void updateMetrics() {
- Component host = ((View)this).getContainer();
- Font f = host.getFont();
- this.metrics = host.getFontMetrics(f);
- this.tabSize = this.getTabSize() * this.metrics.charWidth('m');
- }
- }
-